﻿using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace MyGame
{
    public class Chess : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler
    {
        [Header("行")]
        public int row;

        [Header("列")]
        public int arr;

        private Vector2Int myVector;

        private bool isClick;
        public int chessColor = 0;
        private Image myImage;

        private void Awake()
        {
            myImage = GetComponent<Image>();
        }

        private void Start()
        {
            isClick = false;
            myVector.x = row;
            myVector.y = arr;
        }

        public void Init(Sprite _sprite)
        {
            myImage.sprite = _sprite;
            chessColor = 0;
            isClick = false;
        }

        public void OnPointerClick(PointerEventData eventData)
        {
            if (!isClick && GameManager.Instance.isGaming)
            {
                    isClick = true;              
                    switch (GameManager.Instance.GetExecutor())
                    {
                        case GameManager.Executor.black:
                        ClickBlack();
                        break;
                        case GameManager.Executor.white:
                        ClickWhite(); 
                        break;
                    }               
            }      
        }

        public void OnPointerEnter(PointerEventData eventData)
        {
            if (!isClick && GameManager.Instance.isGaming)
            {
                myImage.sprite = ChessManager.Instance.GetSprite(ChessType.select);
            }
        }

        public void OnPointerExit(PointerEventData eventData)
        {
            if (!isClick)
            {
                myImage.sprite = ChessManager.Instance.GetSprite(ChessType.none);
            }
        }

        public void ClickBlack()
        {
            ChessManager.Instance.AddBlackNumber(this);
            MapManager.Instance.UpdateMap(row, arr, 1);
            myImage.sprite = ChessManager.Instance.GetSprite(ChessType.black);
            chessColor = 1;
            if (MapManager.Instance.IsWingsBlack(row, arr))
            {
                GameManager.Instance.EndGame();
                BoardUI.Instance.GamaOver(1);
                BackGroundUI.Instance.GamaOver();
                Debug.Log("黑方胜");
                return;
            }

            ChangePlayer();
            if (GameManager.Instance.GetGameModel() == GameManager.GameModel.pve)
            {             
                AIPlay();
            }
        }
        
        public void ClickWhite()
        {
            ChessManager.Instance.AddWhiteNumber(this);
            MapManager.Instance.UpdateMap(row, arr, 2);
            myImage.sprite = ChessManager.Instance.GetSprite(ChessType.white);
            chessColor = 2;
            if (MapManager.Instance.IsWingsWhite(row, arr))
            {
                GameManager.Instance.EndGame();
                BoardUI.Instance.GamaOver(2);
                BackGroundUI.Instance.GamaOver();
                Debug.Log("白方胜");
                return;
            }
      
            ChangePlayer();
            if (GameManager.Instance.GetGameModel() == GameManager.GameModel.pve)
            {
                isClick = true;
            }
        }

        void ChangePlayer()
        {
            switch (GameManager.Instance.GetExecutor())
            {
                case GameManager.Executor.none:
                    break;
                case GameManager.Executor.black:
                    GameManager.Instance.SetExecutor(GameManager.Executor.white);
                    BackGroundUI.Instance.ChangeExecutor("白旗");
                    break;
                case GameManager.Executor.white:
                    GameManager.Instance.SetExecutor(GameManager.Executor.black);
                    BackGroundUI.Instance.ChangeExecutor("黑旗");
                    break;
            }          
        }

        void AIPlay()
        {
            switch (GameManager.Instance.GetGameModel())
            {
                case GameManager.GameModel.none:
                    break;
                case GameManager.GameModel.pve:
                    AIManager.Instance.Scoring();
                    break;
                case GameManager.GameModel.pvp:
                    break;
            }
        }
    }
}


